package infomanage.view;

import infomanage.bean.Customer;
import infomanage.service.CustomerList;
import infomanage.utils.CMUtility;

public class CustomerView {
    private CustomerList customerList = new CustomerList(10);

    public static void main(String[] args) {
        CustomerView customerView = new CustomerView();
        customerView.enterMainMenu();
    }

    public void enterMainMenu() {
        while (true) {
            System.out.println("-----------------客户信息管理软件-----------------");
            System.out.println();
            System.out.println("                 1 添 加 客 户");
            System.out.println("                 2 修 改 客 户");
            System.out.println("                 3 删 除 客 户");
            System.out.println("                 4 客 户 列 表");
            System.out.println("                 5 退      出");
            System.out.println();
            System.out.print("                 请选择(1-5)：");

            switch (CMUtility.readMenuSelection()) {
                case '1' -> addNewCustomer();
                case '2' -> modifyCustomer();
                case '3' -> deleteCustomer();
                case '4' -> listAllCustomers();
                case '5' -> {
                    System.out.print("请确认是否退出(Y/N)：");
                    if (CMUtility.readConfirmSelection() == 'Y') {
                        return;
                    }
                }
            }
        }
    }


    private void addNewCustomer() {
        System.out.println("-------------------添加客户-------------------");
        System.out.print("姓名：");
        String name = CMUtility.readString(10);
        System.out.print("性别（男/女）：");
        char gender;
        while (true) {
            gender = CMUtility.readChar();
            if (gender == '男' || gender == '女') {
                break;
            } else {
                System.out.println("请输入男/女：");
            }
        }

        System.out.print("年龄：");
        int age = CMUtility.readInt();

        System.out.print("电话：");
        String phone = CMUtility.readString(11);
        System.out.print("邮箱：");
        String email = CMUtility.readString(30);
        Customer customer = new Customer(name, gender, age, phone, email);

        if (customerList.addCustomer(customer)) {
            System.out.println("---------------------添加完成---------------------\n");
        } else {
            System.out.println("---------------------添加失败---------------------\n");
        }
    }

    private void modifyCustomer() {
        System.out.println("-------------------添加客户-------------------");
        System.out.print("请选择待修改客户编号(-1退出)：");
        int index = CMUtility.readInt();
        if (index == -1) {
            return;
        }
        Customer customer = customerList.getCustomer(index - 1);
        if (customer != null) {
            System.out.printf("姓名（%s）:", customer.getName());
            customer.setName(CMUtility.readString(10, customer.getName()));
            System.out.printf("性别（%s）:", customer.getGender());
            customer.setGender(CMUtility.readChar(customer.getGender()));
            System.out.printf("年龄（%d）:", customer.getAge());
            customer.setAge(CMUtility.readInt(customer.getAge()));
            System.out.printf("电话（%s）:", customer.getPhone());
            customer.setPhone(CMUtility.readString(11, customer.getPhone()));
            System.out.printf("邮箱（%s）:", customer.getEmail());
            customer.setEmail(CMUtility.readString(30, customer.getEmail()));
        }else{
            System.out.println("-------------------修改失败-------------------\n");
            return;
        }

        if (customerList.replaceCustomer(index - 1, customer)) {
            System.out.println("-------------------修改完成-------------------\n");
        } else {
            System.out.println("-------------------修改失败-------------------\n");
        }
    }

    private void deleteCustomer() {
        System.out.println("-------------------删除客户-------------------");
        System.out.print("请选择待删除客户编号(-1退出)：");
        int index = CMUtility.readInt();
        if (index == -1) {
            return;
        }
        System.out.print("确认是否删除(Y/N)：");
        if (CMUtility.readConfirmSelection() == 'N') {
            return;
        }
        if (customerList.deleteCustomer(index - 1)) {
            System.out.println("-------------------删除完成-------------------\n");
        } else {
            System.out.println("-------------------删除失败-------------------\n");
        }
    }

    private void listAllCustomers() {
        System.out.println("-------------------客户列表-------------------");
        if (customerList.getTotal() == 0) {
            System.out.println("没有客户记录！");
            return;
        }
        System.out.println("编号\t姓名\t性别\t年龄\t电话\t邮箱\t");
        int i = 1;
        for (Customer customer : customerList.getAllCustomers()) {
            System.out.println(i++ + "\t" + customer.getName() + "\t" +
                    customer.getGender() + "\t"
                    + customer.getAge() + "\t"
                    + customer.getPhone() + "\t"
                    + customer.getEmail() + "\t");
        }
        System.out.println("------------------客户列表完成------------------\n");
    }
}
